﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Novel.DAL.Base;
using System.Data;
using System.Data.SqlClient;
using Novel.Entity;

namespace Novel.DAL
{
   public class T_001GatherTaskLog_DAL : IT_001GatherTaskLogManager
    {
         #region 表名
        public  string GetTableName()
        {
            return "T_001GatherTaskLog";
        }
        #endregion


        #region IEntityFacade 成员
        /// <summary>
        /// 填充数据
        /// </summary>
        /// <param name="reader"></param>
        public EntityBase FillDetailWithReader(IDataReader reader)
        {
            T_001GatherTaskLogEntity model = new T_001GatherTaskLogEntity();
            if (reader["ID"] != DBNull.Value)
                model.ID = (int)reader["ID"];
            if (reader["BookID"] != DBNull.Value)
                model.BookID = (int)reader["BookID"];
            if (reader["BookName"] != DBNull.Value)
                model.BookName = (string)reader["BookName"];
            if (reader["TableName"] != DBNull.Value)
                model.TableName = (string)reader["TableName"];
            if (reader["ChaptersCount"] != DBNull.Value)
                model.ChaptersCount = (int)reader["ChaptersCount"];
            if (reader["CmfuInfoUrl"] != DBNull.Value)
                model.CmfuInfoUrl = (string)reader["CmfuInfoUrl"];
            if (reader["CmfuListUrl"] != DBNull.Value)
                model.CmfuListUrl = (string)reader["CmfuListUrl"];
            if (reader["CmfuChapterCount"] != DBNull.Value)
                model.CmfuChapterCount = (int)reader["CmfuChapterCount"];
            if (reader["UpdateStatus"] != DBNull.Value)
                model.UpdateStatus = (T_001GatherTaskLogEntity.EnumUpdateStatus)(byte)reader["UpdateStatus"];
            if (reader["UpdateTime"] != DBNull.Value)
                model.UpdateTime = (DateTime)reader["UpdateTime"];
            if (reader["TaskID"] != DBNull.Value)
                model.TaskID = (int)reader["TaskID"];
            if (reader["LastChapterUrl"] != DBNull.Value)
                model.LastChapterUrl = (string)reader["LastChapterUrl"];
            if (reader["LastChapterTitle"] != DBNull.Value)
                model.LastChapterTitle = (string)reader["LastChapterTitle"];
            return model;
        }
        #endregion

        public T_001GatherTaskLogEntity GetNovelbyName(string bookName)
        {
            SQLHelper da = new SQLHelper();
            string strSql = string.Format("SELECT  * FROM {0} where BookName='{1}' ", GetTableName(), bookName);
            SQLHelper.MappEntity<T_001GatherTaskLogEntity> dele_Entity = delegate(SqlDataReader reader)
            {
                return (T_001GatherTaskLogEntity)FillDetailWithReader(reader);
            };
            return da.Exceute<T_001GatherTaskLogEntity>(new SqlCommand(strSql), dele_Entity);

        }
        public List<T_001GatherTaskLogEntity> GetNovelurls()
        {
            SQLHelper da = new SQLHelper();
            string strSql = string.Format("SELECT  * FROM {0} where UpdateStatus=1 and SaveListUrl=1 and ChaptersCount=0", GetTableName());
            SQLHelper.MappEntity<T_001GatherTaskLogEntity> dele_Entity = delegate(SqlDataReader reader)
            {
                return (T_001GatherTaskLogEntity)FillDetailWithReader(reader);
            };
            return da.ExceuteList<T_001GatherTaskLogEntity>(new SqlCommand(strSql), dele_Entity);

        }

        public List<T_001GatherTaskLogEntity> GetUpdateNovels()
        {
            SQLHelper da = new SQLHelper();
            string strSql = string.Format("SELECT  * FROM {0} a where CmfuChapterCount IS   NULL and UpdateStatus=1 order by ID", GetTableName());
            SQLHelper.MappEntity<T_001GatherTaskLogEntity> dele_Entity = delegate(SqlDataReader reader)
            {
                return (T_001GatherTaskLogEntity)FillDetailWithReader(reader);
            };
            return da.ExceuteList<T_001GatherTaskLogEntity>(new SqlCommand(strSql), dele_Entity);

        }

       public DataTable GetUpdateNovelList()
       {

           string strSql = string.Format("SELECT  * FROM {0} where UpdateStatus=1", GetTableName());
           return SQLHelper.Execute(strSql.ToString());
       }
       /// <summary>
       /// 新书列表
       /// </summary>
       /// <returns></returns>
       public DataTable GetNewNovelList()
       {

           string strSql = string.Format("SELECT  * FROM {0} where TaskID=0", GetTableName());
           return SQLHelper.Execute(strSql.ToString());
       }
       /// <summary>
       /// 新书数量
       /// </summary>
       /// <returns></returns>
       public int GetNewNovelCount()
       {

           string strSql = string.Format("SELECT  count(ID) FROM {0} where TaskID=0 ", GetTableName());
           SQLHelper sql=new SQLHelper();

           return (int)sql.ExecuteScalar(new SqlCommand(strSql)); ;
       }
       #region 更新数据
       /// <summary>
       /// 更新状态
       /// </summary>
       /// <returns></returns>
       public int UpdateStatus(T_001GatherTaskLogEntity model)
       {
           StringBuilder strSql = new StringBuilder();
           strSql.AppendFormat("UPDATE {0} SET ", GetTableName());
           strSql.Append("ChaptersCount=@ChaptersCount,TaskID=@TaskID,LastChapterUrl=@LastChapterUrl,UpdateTime=getdate(),UpdateStatus=@UpdateStatus,LastChapterTitle=@LastChapterTitle");
           strSql.Append(" WHERE BookID=@BookID");
           SqlCommand cmd = new SqlCommand(strSql.ToString());
           cmd.Parameters.Add("@BookID", SqlDbType.Int, 4);
           cmd.Parameters["@BookID"].Value = model.BookID;
           cmd.Parameters.Add("@LastChapterUrl", SqlDbType.NVarChar);
           cmd.Parameters["@LastChapterUrl"].Value = model.LastChapterUrl;
           cmd.Parameters.Add("@ChaptersCount", SqlDbType.Int, 4);
           cmd.Parameters["@ChaptersCount"].Value = model.ChaptersCount;
           cmd.Parameters.Add("@TaskID", SqlDbType.Int, 4);
           cmd.Parameters["@TaskID"].Value = model.TaskID;
           cmd.Parameters.Add("@UpdateStatus", SqlDbType.TinyInt, 1);
           cmd.Parameters["@UpdateStatus"].Value = model.UpdateStatus;
           cmd.Parameters.Add("@LastChapterTitle", SqlDbType.NVarChar);
           cmd.Parameters["@LastChapterTitle"].Value = model.LastChapterTitle;
           SQLHelper da = new SQLHelper();
           return da.ExecuteNonQuery(cmd);
       }

         /// <summary>
       /// 标示图书地址已获取
       /// </summary>
       /// <returns></returns>
       public int UpdateListUrlStatus(int BookID)
       {
           StringBuilder strSql = new StringBuilder();
           strSql.AppendFormat("UPDATE {0} SET ", GetTableName());
           strSql.Append("SaveListUrl=1");
           strSql.Append(" WHERE BookID=@BookID");
           SqlCommand cmd = new SqlCommand(strSql.ToString());
           cmd.Parameters.Add("@BookID", SqlDbType.Int, 4);
           cmd.Parameters["@BookID"].Value = BookID;
         
           SQLHelper da = new SQLHelper();
           return da.ExecuteNonQuery(cmd);
       }

       #region 分页数据集
       public DataTable ShowTable(PageEntity entity, out int totalPages, out int totalCount)
       {
           QueryBase manager = new QueryBase();

           entity.tbName = GetTableName();
           DataTable dt = manager.GetTable(entity);
           totalPages = entity.TotalPages;
           totalCount = entity.TotalCount;
           return dt;
       }
       #endregion
       #endregion
    }
}
